const cloud = require('wx-server-sdk');
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
const db = cloud.database();

exports.main = async (event, context) => {
  const { oldPassword, newPassword } = event;
  try {
    const res = await db.collection('config').doc('app_password').get();
    if (res.data.value !== oldPassword) {
      return { success: false, message: '旧密码不正确' };
    }
    await db.collection('config').doc('app_password').update({
      data: { value: newPassword }
    });
    return { success: true, message: '密码修改成功' };
  } catch (e) {
    return { success: false, message: '服务器错误' };
  }
};